package com.fjc.controller;

import com.fjc.base.ResponseObject;
import com.fjc.domain.User;
import com.fjc.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by fjc on 2018/5/22.
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResponseObject login(User user, HttpServletRequest request){
        System.out.println(user);
        return userService.login(user,request);
    }

    @GetMapping("/logout")
    public void logout(HttpServletRequest request){
        System.out.println("退出登陆");
        request.getSession().invalidate();
    }

    @GetMapping("/getUserInfo")
    public ResponseObject getUserInfo(HttpServletRequest request){
        if(request.getSession().getAttribute("user") != null){
            User user = (User)request.getSession().getAttribute("user");
            return new ResponseObject(00,"",user);
        }else{
            return new ResponseObject(-1,"请重新登陆");
        }
    }

    @PostMapping("/create")
    public String createUser(User user){
        userService.createUser(user);
        return "success";
    }
}
